debug = false;
var myTree;
var colorIndex=0;

String.prototype.trim = function() { 
	return this.replace(/^\s+|\s+$/g, '');
};
String.prototype.rtrim = function(ch) {
	return this.replace('/'+ch+'+$/','');
}



function xlistTag(tagname)
{
	text = "<td><div id=tagsList>"+tagname+"<a href=\"javascript:removeTag(\'"+tagname+"\')\">x</a></div></td>";
	return text;
}

function xaddTag()
{
	text = "<table><td><div id=tagsList><a href=\"javascript:addTag()\">&nbsp;+&nbsp;</a></div></td>";
	return text;
};

function getFolder(name)
{
	if (name=="")
  	{
  		return;
  	} 
	
	var url = "search.php";
	var text = "";

	text = "<search>";
	text = text + "<dir>" + name + "</dir>";
	text = text + "</search>";

	ajaxRequest(url, text, name);
}

function getTags(name)
{
	if (name=="")
  	{
  		return;
  	} 
	
	var url = "search.php";
	var text = "";

	text = "<search>";
	text = text + "<filename>" + name + "</filename>";
	text = text + "</search>";

	ajaxRequest(url, text, name);
};


function updateTree(root, updatelem)
{
	var foldername;
	delete myTree;
	myTree = new dTree('myTree');
	myTree.config.useCookies=true;

	myTree.add('/',-1,'root', '/', 'javascript:myTree.openTo(\'/\', true)');
/*
	for (var x=0; x<root.childNodes.length; x++ )
	{
		if(root.childNodes[x].tagName == "dir")
		{
			foldername = root.childNodes[x].childNodes[0].nodeValue;
			if(foldername!="")
			{
				myTree.addUptoParent(foldername);
			}
		}
		else
		{
			filename = root.childNodes[x].childNodes[0].nodeValue;
			openLnk = "javascript:openLink(\'"+ filename +"\')";
			myTree.addFile(filename, foldername, 'link', filename, openLnk);
		}
	}
*/
	var x=root.firstChild;
	while(x) {
		if(x.tagName == "dir")
		{
			foldername = x.firstChild.nodeValue;
			if(foldername!="")
			{
				myTree.addUptoParent(foldername);
			}
		}
		else
		{
			filename = x.firstChild.nodeValue;
			openLnk = "javascript:openLink(\'"+ filename +"\')";
			myTree.addFile(filename, foldername, 'link', filename, openLnk);
		}
		x = x.nextSibling;
		
	}

	updatelem.innerHTML = myTree;

	if(debug) alert(myTree);
	return;
};

function showHideDirTree()
{
	if(document.getElementById("dirtree"))
		closeDirTree();
	else
		openDirTree();
};

var backup="";

function openDirTree()
{
	updatelem = document.getElementById("container");
	if(backup=="")
		updatelem.innerHTML =  "<div id=\"dirtree\">" + myTree + "</div>";
	else
		updatelem.innerHTML =  backup;

	updatelem = document.getElementById("sh_tree");
	updatelem.innerHTML = "[Hide]";

	changeWidth("inframe", inframeW);
	changeHeight("inframe", inframeH);

	changeWidth("page", pageW);
	changeHeight("page", pageH);
};

function closeDirTree()
{
	updatelem = document.getElementById("container");
	backup = updatelem.innerHTML;
	updatelem.innerHTML = "";

	updatelem = document.getElementById("sh_tree");
	updatelem.innerHTML = "[Show]";

	changeWidth("inframe", pageW-40);
	changeHeight("inframe", inframeH);
};

function getRandomColor()
{
	var ColorValue = "#FFFFFF"; // default color - white (colorIndex = 0)

	if(colorIndex == 1)
		ColorValue = "#FFCCCC"; //peach
	else if(colorIndex == 2)
		ColorValue = "#CCAFFF"; //violet
	else if(colorIndex == 3)
		ColorValue = "#A6BEFF"; //lt blue
	else if(colorIndex == 4)
		ColorValue = "#99FFFF"; //cyan
	else if(colorIndex == 5)
		ColorValue = "#D5CCBB"; //tan
	else if(colorIndex == 6)
		ColorValue = "#99FF99"; //lt green
	else if(colorIndex == 7)
		ColorValue = "#FFFF99"; //lt yellow
	else if(colorIndex == 8)
		ColorValue = "#FFCC99"; //lt orange
	else if(colorIndex == 9)
		ColorValue = "#DDDDDD"; //lt grey

	colorIndex += 1;
	colorIndex = colorIndex%10;

	return ColorValue;
}

function startMovingLeft() {
	tl=setTimeout("startMovingLeft()",100);
	moveLeft();
}
function stopMovingLeft() {
	clearTimeout(tl);
}

function startMovingRight() {
	tr=setTimeout("startMovingRight()",100);
	moveRight();
}
function stopMovingRight() {
	clearTimeout(tr);
}

function moveLeft(){
	decreaseWidth('inframe');
	increaseWidth('dirtree');
}

function moveRight(){
	decreaseWidth('dirtree');
	increaseWidth('inframe');
}


function increaseWidth(id){
	e = document.getElementById(id);
	e.width = e.width+10;
	changeWidth(id,e.width);
}

function decreaseWidth(id){
	e = document.getElementById(id);
	e.width = e.width-10;
	changeWidth(id,e.width);
}

function changeWidth(id, x){
	e = document.getElementById(id);
	e.style.width = x+"px";
	e.width = x;
}

function changeHeight(id, x){
	e = document.getElementById(id);
	e.style.height = x+"px";
	e.height = x;
}

function updateResponse(root, id)
{
	if( root.nodeName == "dirtree" )
	{
		updatelem    = document.getElementById("dirtree");
		updateTree(root, updatelem);
	}
	else if(root.nodeName == "files")
	{
		updatelem    = document.getElementById("dirtree");

		for( x=0; x<root.childNodes.length; x++ )
		{
			filename = root.childNodes[x].childNodes[0].nodeValue;
			openLnk = "javascript:openLink(\'"+ filename +"\')";
			myTree.add(filename, id, 'link', filename, openLnk);
		}

		updatelem.innerHTML = myTree;
		return;
	}
	else if(root.nodeName == "tagslist")
	{
		updatelem    = document.getElementById("footer");

		text = xaddTag();
		for( x=0; x<root.childNodes.length; x++ )
		{
			tagname = root.childNodes[x].childNodes[0].nodeValue;
			text += xlistTag(tagname);
		}
		text += "</table>";
		updatelem.innerHTML = text;
		return;
	}
};

function openedPage()
{
	openPage = document.getElementById('innerframe').src;
	openPage.rtrim('/');
	return openPage;
}

function addTag()
{
	tagname = prompt("Type the new tag here ...", "");
	if(!tagname || tagname=="")
		return;
	if(!confirm("Adding tag "+ tagname +" for \"" + openedPage() + "\""))
		return;

	processTagCmd("add", tagname, openedPage() );
};

function removeTag(tagname)
{
	if(!confirm("Removing tag "+ tagname +" for \"" + openedPage() + "\""))
		return;

	processTagCmd("remove", tagname, openedPage() );
}

function processTagCmd(command, tagname, filename)
{
	if (tagname=="")
  	{
  		return;
  	} 
	
	var url = "search.php";
	var text = "";

	text = "<search>";
	text = text + "<command>" + command + "</command>";
	text = text + "<tag>" + tagname + "</tag>";
	text = text + "<file>" + filename + "</file>";
	text = text + "</search>";

	if(debug) alert(text);
	ajaxRequest(url, text, filename);
	getTags(openedPage() );
}


function refresh() {
	getFolder("/");

	changeWidth("page", pageW);
	changeHeight("page", pageH);

	changeWidth("inframe", inframeW);
	changeHeight("inframe", inframeH);

	changeWidth("dirtree", dirtreeW);
	changeHeight("dirtree", dirtreeH);
};

function mySearch()
{
	var searchValue = document.getElementById('search').value;
	var searchUrl   = "http://www.google.com/search?q=" +searchValue+ "&ie=UTF-8&oe=UTF-8";
	openLink(searchUrl);
}
function loadLink()
{
	//alert(document.getElementById('filename').value);
	openLink(document.getElementById('filename').value);
}

function openLink(lnkname)
{
	//alert(lnkname);
	document.getElementById('innerframe').src = lnkname;
	document.getElementById('filename').value   = lnkname;
	getTags(lnkname);
};

function openedNode(folder)
{
	document.getElementById('foldername').value=folder;
};
